<?php

class ConfigArea extends Controller{

    private $_data;
    
    public function __construct(){
        parent::__construct();
        $this->_data = Array();
    }

    private function _render () {
        $this->_data['title'] = 'Gesti&oacute;n de &aacute;rea';
        $this->_data['page'] = 'configArea';
        
        $this->_view->rendertemplate('header',$this->_data);
        $this->_view->rendertemplate('menu',$this->_data);
        $this->_view->render('config/area',$this->_data);
        $this->_view->rendertemplate('footer',$this->_data);
    }
    
    public function index(){
        $this->_data['page_section'] = 'main';
        $this->_render();
    }

    public function area () {
        $this->_view->setOutput (json_encode (Array()));
    }
    
    public function attributes () {
        $idScheme = $_GET['idScheme'];
        $idArea = $_GET['idArea'];
        
        if ($idScheme == "" || $idArea == "") {
            $this->_view->setOutput (json_encode (Array()));
            return;
        }
        
        $response = Array();
        $attributes = $this->_model->getAttributes ($idScheme, $idArea);
        $response['attributes'] = $attributes;
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function areaAttribute () {
        $response = Array();
        
        if (!isset ($_POST['idScheme']) || !isset ($_POST['idArea']) || !isset ($_POST['idAttribute']) || !isset ($_POST['active'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $idScheme = $_POST['idScheme'];
            $idArea = $_POST['idArea'];
            $idAttribute = $_POST['idAttribute'];
            $active = $_POST['active'];
            
            if ($this->_model->setAreaAttribute ($idScheme, $idArea, $idAttribute, $active) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function configurations () {
        $idScheme = $_GET['idScheme'];
        $idArea = $_GET['idArea'];
        
        if ($idScheme == "" || $idArea == "") {
            $this->_view->setOutput (json_encode (Array()));
            return;
        }
        
        $response = Array();
        $configurations = $this->_model->getConfigurations ($idScheme, $idArea);
        
        $response['configurations'] = $configurations;
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function addConfiguration () {
        $response = Array();
        
        if (!isset ($_POST['name']) || !isset ($_POST['order']) || 
                !isset ($_POST['idScheme']) || !isset ($_POST['idArea'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $name = $_POST['name'];
            $order = $_POST['order'];
            $idArea = $_POST['idArea'];
            
            if ($this->_model->addConfiguration ($name, $order, $idArea) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function modifyConfiguration () {
        $response = Array();
        
        if (!isset ($_POST['id']) || !isset ($_POST['name']) || !isset ($_POST['order'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $order = $_POST['order'];
            
            if ($this->_model->modifyConfiguration ($id, $name, $order) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function removeConfiguration () {
        $response = Array();
        
        if (!isset ($_POST['id'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            
            if ($this->_model->removeConfiguration ($id) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
}

?>
